package application;

import java.util.ArrayList;

import CTCOffice.*;
import MBO.*;
import TrackController.*;
import TrackModel.*;
import TrainController.*;
import TrainModel.*;

public class TestMain
{
	public static final long lTick = 500;
	
	public static CTCOffice office;
	public static MBO mbo;
	public static TrackModel trackModel;
	
	public static ArrayList<TrainModel> trainModelList;
	public static ArrayList<TrainController> trainControlList;
	public static ArrayList<TrackController> trackControlList;
	
	
	public static void main(String[] args) throws InterruptedException
	{
		
		office = new CTCOffice();
		mbo = new MBO();
		trackModel = new TrackModel();
		trainModelList = new ArrayList<TrainModel>();
		trainControlList = new ArrayList<TrainController>();
		trackControlList = new ArrayList<TrackController>();
		
		//TrainModel tm = new TrainModel(0,5);
		
		System.out.println(trainModelList);
		
		
		// Main Loop
		while(true)
		{
			// Get the current time
			long millisLongStart = System.currentTimeMillis();
			
			// Give CTC time
			office.tick();
			
			// Give MBO time
			if( office.bMBOEnabled )
			{
				mbo.tick();
			}
			
			
			// Give every track controller time
			for( TrackController currTC : trackControlList)
			{
				currTC.tick();
			}
			
			// Give trackModel time
			trackModel.tick();
			
			
			for( TrainModel currTM : trainModelList )
			{
				currTM.tick();
			}
			
			for( TrainController currTnC : trainControlList )
			{
				currTnC.tick();
			}
			
			// Get the current time
			long millisLongEnd = System.currentTimeMillis();
			
			
			// Calculate sleep
			long sleepLen = lTick - (millisLongEnd - millisLongStart);
			System.out.println("sleepLen: " + sleepLen);
			Thread.sleep(sleepLen);
		}
	}
	
	public static void registerTrainModel( TrainModel tm )
	{
		Main.trainModelList.add(tm);
	}
	
	public static void registerTrainController( TrainController tc )
	{
		trainControlList.add(tc);
	}
	
	public static void registerTrackController( TrackController tc )
	{
		trackControlList.add(tc);
	}

}
